var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
var requestService = require('../../utils/requestService.js');
var page=1;
Page({
    data: {
        tabs: ["未支付","已结束"],
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 0,
        orders:[],
        chargingBill:''/**充电中的账单*/,
        hasMore:true
    },
    onLoad: function () {
        var that = this;
        this.showOrders(0);
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
                    sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
                });
                
            }
        });
       
    },
    tabClick: function (e) {
        var that = this;
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: e.currentTarget.id
        });
        this.showOrders(this.data.activeIndex,);

    },
    loadOrders: function () {
        var that = this;
        // 显示加载图标
        // 页数+1
        page = page + 1;

       this.showOrders(this.data.activeIndex,page);
    },
    onPullDownRefresh:function(){
        wx.showNavigationBarLoading();
        this.showOrders(this.data.activeIndex,0);
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
    },
    showOrders:function (id,page) {
        var that = this;
        if(!page){
            page=1;
        }
        switch ( Number(id)){
            case 0 :{
            requestService.sendGetRequest("/order/unpay?pageNo="+page,null,function (bills) {
                  console.log(bills);
                 
                  if(page==1){
                    that.setData({
                      orders: bills.data.resData,
                      hasMore: true
                    })
                  }else{
                    that.setData({
                      orders: that.data.orders.concat(bills.data.resData)
                    })
                    if (bills.data.resData.length < 1){
                      that.setData({
                        hasMore: false
                      })
                    }
                  }
                   
                });
                break;
            };
            case 1:{
                requestService.sendGetRequest("/order/orders?pageNo="+page, null, function (bills) {

                  if (page == 1) {
                    that.setData({
                      orders: bills.data.resData,
                      hasMore: true
                    })
                  } else {
                    that.setData({
                      orders: that.data.orders.concat(bills.data.resData)
                    })
                    if (bills.data.resData.length < 1) {
                      that.setData({

                        hasMore: false
                      })
                    }
                  }
                });
                break;
            }
        }
    }
});
